﻿using System;
using System.Diagnostics;
using System.ServiceProcess;
using Quartz;
using Quartz.Impl;
using System.Configuration;
using Common.Logging;

namespace Wicresoft.RDW.Service
{
    public partial class RequestService : ServiceBase
    {
        private static ILog log = LogManager.GetLogger(typeof(RequestService));
        public RequestService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            log.Info(" In OnStart");
            //job schedule
            Schedule();
        }

        protected override void OnStop()
        {
            log.Info("service stop");
        }
        private void Schedule()
        {
            try
            {
                ISchedulerFactory schedFact = new StdSchedulerFactory();

                IScheduler sched = schedFact.GetScheduler();

                JobDetail autoApproveJob = new JobDetail("Approve", "group", typeof(RequestMonitor));
                JobDetail feedBackReminderJob = new JobDetail("FeedBackReminder", "group", typeof(RegisterFeedBackRemind));

                Trigger autoApproveTrigger = TriggerUtils.MakeMinutelyTrigger(30);

                autoApproveTrigger.StartTimeUtc = DateTime.UtcNow.AddMinutes(3);
                autoApproveTrigger.Name = "AutoApprove";
                autoApproveTrigger.Group = "group";

                Trigger feedBackReminderTrigger = TriggerUtils.MakeMinutelyTrigger(60);
                feedBackReminderTrigger.StartTimeUtc = DateTime.UtcNow.AddMinutes(2);
                feedBackReminderTrigger.Name = "FeedBackReminder";
                feedBackReminderTrigger.Group = "group";
                  
                sched.Start();
                sched.ScheduleJob(autoApproveJob, autoApproveTrigger);
                sched.ScheduleJob(feedBackReminderJob, feedBackReminderTrigger);
                
                log.Info(" job shcedule add...");
            }
            catch(Exception e)
            {
                log.Error("job shcedule add exception"+e.Message);
            }
        }


        public void TaskStart(string[] args)
        {
            OnStart(args);
        }
    }
}
